<?php

namespace app\common\model;

use think\Db;
use think\Model;

class MenuModel extends Model
{
    //
    public
        $table = 'cms_blog_menu',
        $pk = 'id';

    public function getList()
    {
        $data = Db::query("SELECT * FROM $this->table");
        return $data;
    }

    public function updateData($param)
    {
        if (!empty($param['id'])) {
            $data = [
                'mark' => $param['mark'],
                'name' => $param['name'],
                'parent' => $param['parent'],
                'type' => 'center',
                'icon' => !empty($param['icon']) ? $param['icon'] : '',
                'url' => $param['url'],
                'update_time' => time(),
            ];
        } else {
            $data = [
                'mark' => $param['mark'],
                'name' => $param['name'],
                'parent' => $param['parent'],
                'type' => 'center',
                'icon' => !empty($param['icon']) ? $param['icon'] : '',
                'url' => $param['url'],
                'create_time' => time(),
            ];
        }
        if (!empty($param['id'])) {
            $sql = $this->save($data, ['id' => $param['id']]);
            if ($sql) {
                return ['status' => 1, 'message' => '修改成功'];
            } else {
                return ['status' => 0, 'message' => '修改失败'];
            }
        } else {
            $sql = $this->insert($data);
            if ($sql) {
                return ['status' => 1, 'message' => '添加成功'];
            } else {
                return ['status' => 0, 'message' => '添加失败'];
            }
        }
    }

    public function del($id)
    {
        try {
            $data = $this->destroy($id);
            if ($data) {
                return ['status' => 1, 'message' => '删除成功'];
            } else {
                return ['status' => 0, 'message' => '删除失败'];
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
        }

    }
}
